package org.sgx.gwteditors.testgallery.gallery;

import java.util.Map;

import org.sgx.gwteditors.client.editor.props.PropertyHaver;
import org.sgx.gwteditors.client.editor.props.PropertyHaverUtil;
import org.sgx.gwteditors.client.editor.props.PropertyInfo;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor1;
import org.sgx.gwteditors.client.util.LWOMap;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PropertyHaverEditor1TTest1 implements Test {

	/** the java class (a PropertyHaver) to be edited in this test. Exactly the same as in previus example
	 * @author sg
	 */
	public static class Person implements PropertyHaver {		

		//what follows is a common java bean definition
		
		String name;
		Integer age;
		Boolean male;
		
		//getters & setters, not neccesary...
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Integer getAge() {
			return age;
		}
		public void setAge(Integer age) {
			this.age = age;
		}
		public Boolean getMale() {
			return male;
		}
		public void setMale(Boolean male) {
			this.male = male;
		}		
		
		
		//what follows is the part we need to implement PropertyHaver for the neccesary reflection info
		// property types definition
		static private LWOMap<String, PropertyInfo> _props = 
				PropertyHaverUtil.buildPropMap(new PropertyInfo[]{
						new PropertyInfo("age", EditorFramework1.TYPE_INTEGER),
						new PropertyInfo("name", EditorFramework1.TYPE_STRING),
						new PropertyInfo("male", EditorFramework1.TYPE_BOOLEAN)
				});
		
				
		public LWOMap<String, PropertyInfo> getProperties() {
			return _props;
		}		
		
		//property accessors
		@Override
		public Object propertyGet(String prop) {			
			if(prop.equals("age"))
				return age;
			else if(prop.equals("name"))
				return name;
			else if(prop.equals("male"))
				return male;
			else return null;
		}
		
		@Override
		public void propertySet(String prop, Object val) {
			if(prop.equals("age"))
				setAge((Integer) val);
			else if(prop.equals("name"))
				setName((String)val);
			else if(prop.equals("male"))
				setMale((Boolean)val);
		}	
	}
	
	
	private PropertyHaverEditor1<Person> ed;
	
	@Override
	public void test(Panel parent) {
		new EditorFramework1().start();
		
		Person p1 = new Person();
		p1.setName("sebastián");
		p1.setAge(28);
		p1.setMale(true);
		
		ed = new PropertyHaverEditor1<Person>();
		
		
		Button b1 = new Button("edit!", new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				Person edited = ed.flush();
				Window.alert("edited name : "+edited.getName());
			}
		});		
		
		VerticalPanel vp = new VerticalPanel();
		parent.add(vp);		
		vp.add(ed.getWidget());
		vp.add(b1);
		ed.load(p1);
	}

	//test stuff
	@Override
	public String getName() {
		return "PH editor";
	}
	
//	@Override
//	public String[] getJavaSources() {
//		return new String[]{TestResources.INSTANCE.PropertyHaverEditor1TTest1().getText()};
//	}
	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.PropertyHaverEditor1TTest1()); 
	}
	@Override
	public String[] getTags() {
		return new String[]{};
	}
	@Override
	public String getDescription() {
		return "this test shows a simple way of defining a java class for " +
			"being edited and how to show a table oriented editor " +
			"so the GUI user can edit this kind of java objects. NOte 2 : in thi case we implement ProperyHaver by hand, not using the high level java bean oriented EditableBean tool.";
	}
}
